#pragma once
#include <assert.h>
#include <iostream>
using namespace std;
namespace apex {
template <class T> class vector {
public:
    typedef T* iterator;
    typedef const T* const_iterator;

private:
    iterator _start;
    iterator _finish;
    iterator _end_of_storage;

public:
    iterator begin() { return _start; }
    const_iterator begin() const { return _start; }
    iterator end() { return _finish; }
    const_iterator end() const { return _finish; }

    vector() : _start(nullptr), _finish(nullptr), _end_of_storage(nullptr) {}

    vector(size_t n, const T& val = T())
        : _start(nullptr), _finish(nullptr), _end_of_storage(nullptr) {
        reserve(n);
        for (size_t i = 0; i < n; ++i) {
            push_back(val);
        }
    }

    template <class InputIterator>
    vector(InputIterator first, InputIterator last)
        : _start(nullptr), _finish(nullptr), _end_of_storage(nullptr) {
        while (first != last) {
            push_back(*first);
            ++first;
        }
    }

    void swap(vector<T>& v) {
        std::swap(_start, v._start);
        std::swap(_finish, v._finish);
        std::swap(_end_of_storage, v._end_of_storage);
    }
    vector(const vector<T>& v)
        : _start(nullptr), _finish(nullptr), _end_of_storage(nullptr) {
        vector<T> tmp(v.begin(), v.end());
        swap(tmp);
    }

    vector<T>& operator=(vector<T> v) {
        swap(v);
        return *this;
    }

    ~vector() {
        delete[] _start;
        _start = _finish = _end_of_storage = nullptr;
    }

    size_t capacity() const { return _end_of_storage - _start; }
    size_t size() const { return _finish - _start; }

    T& operator[](size_t pos) {
        assert(pos < size());

        return _start[pos];
    }
    const T& operator[](size_t pos) const {
        assert(pos < size());

        return _start[pos];
    }

    void reserve(size_t n) {
        if (n > capacity()) {
            size_t sz = size();
            T* tmp = new T[n];

            if (_start != nullptr) {
                for (size_t i = 0; i < sz; ++i) {
                    tmp[i] = _start[i];
                }
                delete[] _start;
            }

            _start = tmp;
            _finish = _start + sz;
            _end_of_storage = _start + n;
        }
    }
    void resize(size_t n, const T& val = T()) {
        if (n > capacity()) {
            reserve(n);
        }
        if (n > size()) {
            while (_finish < _start + n) {
                *_finish = val;
                ++_finish;
            }
        } else {
            _finish = _start + n;
        }
    }

    void push_back(const T& x) { insert(end(), x); }

    void pop_back() {
        assert(_finish > _start);
        --_finish;
    }

    iterator insert(iterator pos, const T& x) {
        assert(pos >= _start && pos <= _finish);

        if (_finish == _end_of_storage) {
            size_t len = pos - _start;
            reserve(capacity() == 0 ? 4 : capacity() * 2);
            pos = _start + len;
        }

        iterator end = _finish - 1;
        while (end >= pos) {
            *(end + 1) = *end;
            --end;
        }
        *pos = x;
        ++_finish;
        return pos;
    }

    iterator erase(iterator pos) {
        assert(pos >= _start);
        assert(pos < _finish);

        iterator begin = pos + 1;
        while (begin < _finish) {
            *(begin - 1) = *begin;
            ++begin;
        }

        --_finish;

        if (size() < capacity() / 2) {
        }

        return pos;
    }

    T& front() {
        assert(size() > 0);
        return *_start;
    }

    T& back() {
        assert(size() > 0);
        return *(_finish - 1);
    }
};
} // namespace apex
